package cn.topcodes.tcsf.admin.web;

import cn.topcodes.tcsf.admin.commons.generic.event.EntityDeletedEvent;
import cn.topcodes.tcsf.admin.domain.entity.RatelimiterRule;
import org.apache.http.protocol.UriPatternMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;

/**
 * Created by Unicorn on 2018/3/8.
 */
public class RateLimiterRuleDeletedListener implements ApplicationListener<EntityDeletedEvent> {

    @Autowired
    private RateLimiterFilter rateLimiterFilter;

    @Override
    public void onApplicationEvent(EntityDeletedEvent event) {
        if(event.getEntity() instanceof RatelimiterRule) {
            RatelimiterRule rule = (RatelimiterRule) event.getEntity();
            UriPatternMatcher uriPatternMatcher = rateLimiterFilter.getUriPatternMatcher();
            uriPatternMatcher.unregister(rule.getUri());
        }

    }
}
